package cn.db101.fairy.controller

import cn.db101.fairy.component.Slf4j
import cn.db101.fairy.entity.ChineseCharacter
import cn.db101.fairy.model.ResultModel
import cn.db101.fairy.service.HzService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * @author zooy
 * @since 2024/5/19-11:23
 */
@RestController
@RequestMapping("v1/hz")
@Slf4j
class HzController {

    @Autowired
    open lateinit var hzService: HzService

    @GetMapping("search/{name}")
    fun search(@PathVariable("name") name: String): ResponseEntity<Any?> {

        return ResponseEntity.ok().body(hzService.one(name)?.content)
    }

    @GetMapping("random")
    fun random(): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(hzService.random().name!!)
    }

    @PostMapping("update")
    fun update(@RequestBody chineseCharacter: ChineseCharacter): ResultModel<Any> {

        hzService.update(chineseCharacter)
        return ResultModel.wrapSuccessResult()
    }

    @PostMapping("find")
    fun search(@RequestBody chineseCharacter: ChineseCharacter): ResultModel<Any?> {

        return ResultModel.wrapSuccessResult(hzService.one(chineseCharacter.name!!))
    }

}
